

package com.springcloud.sc.ucpv5.client.resource;

import java.util.Locale;
import java.util.MissingResourceException;
import java.util.PropertyResourceBundle;

import com.springcloud.sc.ucpv5.client.util.PublicUtil;

public final class ResourceManager {
	/**
     *
     */
	private String resourceFile;

	/**
     *
     */
	private PropertyResourceBundle resources;

	/**
     *
     */
	private Locale locale;

	/**
	 *
	 * 4-11
	 * 
	 * @param moduleKey
	 * @param resourceFile
	 * @return
	 * @throws ReadResourceException
	 * @see
	 */
	public static ResourceManager getInstance(String moduleKey, String resourceFile) throws ReadResourceException {
		return getInstance(moduleKey, resourceFile, null);
	}

	/**
	 *
	 * 4-11
	 * 
	 * @param moduleKey
	 * @param resourceFile
	 * @param locale
	 * @return
	 * @throws ReadResourceException
	 * @see
	 */
	public static ResourceManager getInstance(String resourceFlag, String resourceFile, Locale locale) throws ReadResourceException {
		ResourceManager resourceManager = new ResourceManager();
		String prefix = "resources" + "." + "zh" + ".";
		resourceManager.setResourceFile(prefix + resourceFile);
		// resourceManager.setResourceFile(resourceFile);
		resourceManager.setLocale(locale);
		resourceManager.loadResource();
		return resourceManager;
	}

	/**
	 *
	 * @author 4-11
	 */
	private ResourceManager() {

	}

	/**
	 *
	 * 4-11
	 * 
	 * @param resourceFile
	 * @see
	 */
	private void setResourceFile(String resourceFile) {
		this.resourceFile = resourceFile;
	}

	/**
	 *
	 * 4-11
	 * 
	 * @return
	 * @see
	 */
	public String getResourceFile() {
		return resourceFile;
	}

	/**
	 *
	 * 4-11
	 * 
	 * @throws ReadResourceException
	 * @see
	 */
	private void loadResource() throws ReadResourceException {
		if (resourceFile == null) {
			throw new ReadResourceException("没有指定资源文件");
		}

		if (locale == null) {
			resources = (PropertyResourceBundle) PropertyResourceBundle.getBundle(getResourceFile());
		} else {
			resources = (PropertyResourceBundle) PropertyResourceBundle.getBundle(getResourceFile(), locale);
		}

		if (resources == null) {
			throw new ReadResourceException("无法定位到资源文件 " + getResourceFile());
		}

		locale = resources.getLocale();
	}

	/**
	 * 采用可变长度参数替代匹配字串的标识符 6-1
	 * 
	 * @param key
	 * @param strings
	 * @return
	 * @see
	 */
	public String getString(String key, String... args) {
		String value = "";
		int argsLength = args.length;
		try {
			value = resources.getString(key);
			for (int i = 0; i < argsLength; i++) {
				value = PublicUtil.replace(value, "{" + i + "}", args[i]);
			}
		} catch (MissingResourceException e) {
			value = key;
		}

		return value;
	}

	/**
	 *
	 * 4-11
	 * 
	 * @return
	 * @see
	 */
	public Locale getLocale() {
		return locale;
	}

	/**
	 *
	 * 4-11
	 * 
	 * @param locale
	 * @see
	 */
	private void setLocale(Locale locale) {
		this.locale = locale;
	}

	/**
     *
     * 
     */
	public String toString() {
		return "[resourceFile = " + getResourceFile() + ", Locale = " + getLocale() + "]";
	}
}
